﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace pswb.Controls
{
    [DefaultPropertyAttribute("Label")]
    public partial class FieldTypeDisplay : UserControl
    {
        #region Events
        public event MouseEventHandler onMouseDown;
        public event EventHandler onSelect;
        public event EventHandler onChange;
        #endregion

        #region Properties
        [Browsable(false)]
        public bool isNew { get; set; }

        private Guid _ID;
        [CategoryAttribute("Identification"),DescriptionAttribute("Unique identifier of this field")]
        public Guid ID
        {
            get { return _ID; }
        }
        

        private string _fieldLabel;

        [CategoryAttribute("Design"),DescriptionAttribute("Label to be displayed on this field")]
        public string Label
        {
            get { return _fieldLabel; }
            set
            {
                _fieldLabel = value;
                lblLabel.Text = value;
                if (onChange != null)
                    onChange(new object(), new EventArgs());
            }
        }

        private int _colIndex;
        [CategoryAttribute("Design"), DescriptionAttribute("Column index")]
        public int Column
        {
            get { return _colIndex; }
            set
            {
                _colIndex = value;
                setColIndex(value);
                if (onChange != null)
                    onChange(new object(), new EventArgs());
            }
        }


        private int _rowIndex;
        [CategoryAttribute("Design"), DescriptionAttribute("Row index")]
        public int Row
        {
            get { return _rowIndex; }
            set
            {
                _rowIndex = value;
                setRowIndex(value);
                if (onChange != null)
                    onChange(new object(), new EventArgs());
            }
        }

        private int _colSpan;
        [CategoryAttribute("Design"), DescriptionAttribute("Column span")]
        public int ColumnSpan
        {
            get { return _colSpan; }
            set
            {
                if (value > 3)
                    value = 3;
                if (value < 1)
                    value = 1;
                _colSpan = value;
                setColSpan(value);
                if (onChange != null)
                    onChange(new object(), new EventArgs());
            }
        }

        private int _rowSpan;
        [CategoryAttribute("Design"), DescriptionAttribute("Row span")]
        public int RowSpan
        {
            get { return _rowSpan; }
            set
            {
                if (value < 1)
                    value = 1;
                _rowSpan = value;
                setRowSpan(value);
                if (onChange != null)
                    onChange(new object(), new EventArgs());
            }
        }

        #endregion

        #region Public Methods
        public void InitializeProperties(int ColIndex, int RowIndex)
        {
            Column = ColIndex;
            Row = RowIndex;
        }

        public void setID(Guid id)
        {
            _ID = id;
        }
        #endregion

        #region Private Methods
        public FieldTypeDisplay()
        {
            InitializeComponent();
        }

        private void FieldTypeDisplay_MouseDown(object sender, MouseEventArgs e)
        {
            onMouseDown(sender, e);
        }

        private void FieldTypeDisplay_Load(object sender, EventArgs e)
        {
            if (isNew)
            {
                _ID = Guid.NewGuid();
            }
            panel1.BackgroundImage = imageList1.Images[2];
            panel1.BackgroundImageLayout = ImageLayout.Center;
        }

        private void panel1_Click(object sender, EventArgs e)
        {
            onSelect(this, e);
        }

        private void panel1_MouseMove(object sender, MouseEventArgs e)
        {
            if (e.Button == System.Windows.Forms.MouseButtons.Left)
                onMouseDown(this, e);
        }

        private void setColSpan(int span)
        {
            TableLayoutPanel tbp = (TableLayoutPanel)this.Parent;
            tbp.SetColumnSpan(this, span);
        }

        private void setRowSpan(int span)
        {
            TableLayoutPanel tbp = (TableLayoutPanel)this.Parent;
            if (span > tbp.RowCount)
                span = tbp.RowCount;
            _rowSpan = span;
            tbp.SetRowSpan(this, span);
        }

        private void setColIndex(int value)
        {
            TableLayoutPanel tbp = (TableLayoutPanel)this.Parent;
            if (value - 1 < tbp.ColumnCount)
                tbp.SetColumn(this, value);
        }

        private void setRowIndex(int value)
        {
            TableLayoutPanel tbp = (TableLayoutPanel)this.Parent;
            if (value < tbp.RowCount)
                tbp.SetRow(this, value);
        }

        private void linkLabel1_MouseEnter(object sender, EventArgs e)
        {
            linkLabel1.BackColor = Color.YellowGreen;
        }

        private void linkLabel1_MouseLeave(object sender, EventArgs e)
        {
            linkLabel1.BackColor = Color.Transparent;
        }

        private void linkLabel1_Click(object sender, EventArgs e)
        {
            TableLayoutPanel tbp = (TableLayoutPanel)this.Parent;
            tbp.Controls.Remove(this);
        }
        #endregion
    }
}
